import { Component, Input, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { myValidNull } from '@core/util/string-utils';
import { MyModalHelper } from '@core/my-modal.helper';
import { CakeOrderViewComponent } from '../../../mid-autumn/cake-order/order-view/cake-order-view.component';
import {
  CAKE_ORDER_SERVICE_STATUS_OPTION,
  CAKE_ORDER_STATUS_OPTION,
  CAKE_ORDER_VOUCHER_STATUS_OPTION,
} from '../../../../constant/dictionary';
import { ProfitRecordService } from '@core/routes-services/profit-record.service';
import { CompanyService } from '@core/routes-services/company.service';

@Component({
  selector: 'app-fans-profits',
  templateUrl: './fans-profits.component.html'
})
export class FansProfitsComponent implements OnInit {
  @Input() type: any = 0;// 0 - 收益菜单列表查所有的 1 - 查用户的收益记录  2 - 查活动的收益记录  默认 1
  @Input() entityId: any = 0;// type = 1 - 对应用户id; type =  2 - 对应活动id
  isLoading = false;
  keyword: any;
  companyId:any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  companyList:any [] = [];
  constructor(private profitRecordService: ProfitRecordService,
              private companyService:CompanyService,
              public message: NzMessageService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit(): void {
    this.initCompanys();
    this.initPage();
  }
  initCompanys(){
    this.isLoading = true;
    if(this.type == 0) {
      this.companyService.getAll().then((res) => {
        if (res.status.toString().startsWith("20")) {
          this.companyList = res.data.companyList;
          console.log(this.companyList)
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error)
        this.isLoading = false;
      });
    }
  }
  initPage() {
    if(this.type == 1){
      this.page['_filter_eq_cusId'] = this.entityId;
    }else if(this.type == 2){
      this.page['_filter_eq_companyId'] = this.entityId;
    }
    this.isLoading = true;
    this.profitRecordService.pagination(this.page, this.keyword, this.companyId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
        console.log(this.page)
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  @Input() allProducts: any [] = [];
  @Input() cakeOrderStatusOption: any [] = CAKE_ORDER_STATUS_OPTION;
  @Input() cakeOrderServiceStatusOption: any [] = CAKE_ORDER_SERVICE_STATUS_OPTION;
  @Input() cakeOrderVoucherStatusOption: any [] = CAKE_ORDER_VOUCHER_STATUS_OPTION;
  seeEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    let statusOption = this.cakeOrderStatusOption;
    if (entity.paymentMethod == 2) {
      statusOption = this.cakeOrderServiceStatusOption;
    }
    if (entity.paymentMethod == 3 || entity.paymentMethod == 4) {
      statusOption = this.cakeOrderVoucherStatusOption;
    }
    this.modalHelper.open(CakeOrderViewComponent, {
      entity: entity.moonCakeReservation,
      allProducts: this.allProducts,
      statusOption: statusOption,
    }, 'lg', {
      nzTitle: '订单信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
}
